using System;
using System.Collections.Generic;
using System.Drawing;

using TTDotNet.Util;
using TTDotNet.Engine.ThreeDEngine;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace TTDotNet.Engine.GUI
{
    public class ViewWindow : Control // NOT DEVICE RESET SAFE!
    {
        private Texture texture;
        private Surface surface;

        private Surface oldSurface;

        private AJCamera windowCamera;

        public ViewWindow(RectangleF screenRect, AJCamera camera)
        {
            _size = screenRect;
            
            camera.ChangeAspectRatio(screenRect.Width, screenRect.Height);

            _position = new PointF(screenRect.X, screenRect.Y);
            _renderPosition.X = _position.X;
            _renderPosition.Y = _position.Y;

            windowCamera = camera;

            SetSize(screenRect.Width, screenRect.Height);
        }

        public override void Render(PointF location)
        {
            try
            {
                oldSurface = Game.device.GetRenderTarget(0);

                Game.device.SetRenderTarget(0, surface);
                Game.device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Aquamarine, 1.0f, 0);

                Matrix proj = Game.device.Transform.Projection;

                Game.gameWorld.Scene.Render(windowCamera);
                Game.device.RenderState.CullMode = Cull.None;

                Game.device.SetRenderTarget(0, oldSurface);

                Game.guiSprite.Transform = Matrix.Identity;
                Game.guiSprite.Draw(texture, Rectangle.Empty, Vector3.Empty, Vector3.Add(_renderPosition, new Vector3(location.X, location.Y, 0)), Color.White);

                Game.device.Transform.World = Matrix.Identity;
                Game.device.Transform.View = Matrix.Identity;
                Game.device.Transform.Projection = proj;

            }
            catch (Exception e)
            {
                SetupResources(_size.Width, _size.Height);
            }
        }

        public override void SetSize(float newWidth, float newHeight)
        {
            _renderPosition.X = _position.X;
            _renderPosition.Y = _position.Y;

            _size.Width = newWidth;
            _size.Height = newHeight;

            windowCamera.ChangeAspectRatio(newWidth, newHeight);

            if(texture != null) texture.Dispose();

            SetupResources(newWidth, newHeight);
        }

        public override void ResizeBy(float widthFactor, float heightFactor)
        {
            _position.X *= widthFactor;
            _position.Y *= heightFactor;
            _renderPosition.X = _position.X;
            _renderPosition.Y = _position.Y;

            SetSize(_size.Width * widthFactor, _size.Height * heightFactor);
        }

        private void SetupResources(float newWidth, float newHeight)
        {
            texture = new Texture(Game.device, (int)newWidth, (int)newHeight, 1, Usage.RenderTarget, Format.X8R8G8B8, Pool.Default);
            surface = texture.GetSurfaceLevel(0);
        }

        public AJCamera Camera
        {
            get { return windowCamera; }
        }

    }
}
